package com.example.newsapp.ui.components.newsComponent

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.navigation.TabNavigation
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 中部新闻显示列表
 */
@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun CenterNewsList(modifier: Modifier = Modifier) {
    // 刷新时间
    LaunchedEffect(StateSave.keyRefresh) {
        launch {
            delay(2000)
            StateSave.isRefresh = false
        }
    }
        // 下拉刷新
    SwipeRefresh(
        state = rememberSwipeRefreshState(StateSave.isRefresh),
        onRefresh = {
            StateSave.initState++
            StateSave.isRefresh = true
            StateSave.keyRefresh++
            when (StateSave.tabNum) {
                0 -> StateSave.currentTab2 = StateSave.tabNum
                1 -> StateSave.currentTab2 = StateSave.tabNum
                2 -> StateSave.currentTab2 = StateSave.tabNum
                3 -> StateSave.currentTab2 = StateSave.tabNum
                4 -> StateSave.currentTab2 = StateSave.tabNum
                5 -> StateSave.currentTab2 = StateSave.tabNum
                6 -> StateSave.currentTab2 = StateSave.tabNum
                7 -> StateSave.currentTab2 = StateSave.tabNum
            }
            when (StateSave.previousTab) {
                0 -> StateSave.previousTab2 = StateSave.previousTab
                1 -> StateSave.previousTab2 = StateSave.previousTab
                2 -> StateSave.previousTab2 = StateSave.previousTab
                3 -> StateSave.previousTab2 = StateSave.previousTab
                4 -> StateSave.previousTab2 = StateSave.previousTab
                5 -> StateSave.previousTab2 = StateSave.previousTab
                6 -> StateSave.previousTab2 = StateSave.previousTab
                7 -> StateSave.previousTab2 = StateSave.previousTab
            }
            Log.e("StateSave.tabNum", StateSave.tabNum.toString())
            Log.e("StateSave.currentTab2", StateSave.currentTab2.toString())
            Log.e("StateSave.previousTab", StateSave.previousTab.toString())
            Log.e("StateSave.previousTab2", StateSave.previousTab2.toString())
        }
    ) {
        // 新闻页面
        TabNavigation()
    }
}